<?php

// +----------------------------------------------------------------------
// | 柠檬开发框架
// +----------------------------------------------------------------------
// | 零门槛、轻代码、响应式、清爽、简洁
// +----------------------------------------------------------------------
// | 本项目基于EasyAdmin进行二次开发
// +----------------------------------------------------------------------
// | 相关技术文档可参考：http://easyadmin.99php.cn/docs/index.html
// +----------------------------------------------------------------------


namespace app\admin\controller\lemon;


use app\admin\traits\Curd;
use app\common\controller\AdminController;
use EasyAdmin\annotation\ControllerAnnotation;
use EasyAdmin\annotation\NodeAnotation;
use think\App;
use DtApp\Bt\BtException;
use DtApp\Bt\System;
use DtApp\Bt\Files;
use DtApp\Bt\Config;
use DtApp\Bt\Ajax;
use DtApp\Bt\Task;

/**
 * Class Baota
 * @package app\admin\controller\lemon
 * @ControllerAnnotation(title="柠檬助手-宝塔面板API接口")
 */
class Baota extends AdminController
{
    /**
     * @NodeAnotation(title="发起请求")
     */
    protected function send()
    {
        $object_name = ucfirst($this->request->action());
        $action_name = $this->request->get('action');
        $config = [
            'key' => sysconfig('baota', 'panel_token_value'),
            'panel' => sysconfig('baota', 'panel_url_value'),
        ];
        try {
            switch ($object_name) {
                case 'System':
                    $object = new System($config);
                    break;
                case 'Files':
                    $object = new Files($config);
                    break;
                case 'Config':
                    $object = new Config($config);
                    break;
                case 'Task':
                    $object = new Task($config);
                    break;
                case 'Ajax':
                    $object = new Ajax($config);
                    break;
                default:
                    return "{$object_name}接口不存在";
                    break;
            }

            $result = $object->$action_name();
            return json($result);
        } catch (BtException $e) {
            return json($e->getMessage());
        }
    }

    /**
     * @NodeAnotation(title="系统")
     */
    public function system()
    {
        return $this->send();
    }

    /**
     * @NodeAnotation(title="文件")
     */
    public function files()
    {
        return $this->send();
    }

    /**
     * @NodeAnotation(title="配置")
     */
    public function config()
    {
        return $this->send();
    }

    /**
     * @NodeAnotation(title="任务")
     */
    public function task()
    {
        return $this->send();
    }

    /**
     * @NodeAnotation(title="AJax")
     */
    public function ajax()
    {
        return $this->send();
    }
}
